local hx__kuijun = fk.CreateSkill {
  name = "hx__kuijun",
  tags = { Skill.Compulsory },
}

hx__kuijun:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__kuijun.name) and data.card and data.card.trueName == "slash" then
      local to = data.to
      return not to.dead and #player:getCardIds{Player.Hand, Player.Equip} > #to:getCardIds{Player.Hand, Player.Equip}
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    if not table.contains(player:getTableMark("@hx__kuijun_select-turn"), 1) then
      table.insert(choices, "#hx__kuijun_damage")
    end
    if not table.contains(player:getTableMark("@hx__kuijun_select-turn"), 2) then
      table.insert(choices, "#hx__kuijun_extra")
    end
    if not table.contains(player:getTableMark("@hx__kuijun_select-turn"), 3) then
      table.insert(choices, "#hx__kuijun_slash")
    end
    if not table.contains(player:getTableMark("@hx__kuijun_select-turn"), 4) then
      table.insert(choices, "#hx__kuijun_get")
    end
    if #choices == 0 then return end
    local choice = room:askToChoice(player,{
      choices = choices,
      skill_name = hx__kuijun.name,
    })
    if choice == "#hx__kuijun_damage" then
      room:addTableMark(player, "@hx__kuijun_select-turn", 1)
      data.additionalDamage = (data.additionalDamage or 0) + 1
      room:sendLog{
        type = "神徐盛选择了此【杀】伤害+1",
        from = player.id,
        arg = hx__kuijun.name
      }
    elseif choice == "#hx__kuijun_extra" then
      room:addTableMark(player, "@hx__kuijun_select-turn", 2)
      data.use.additionalEffect = (data.use.additionalEffect or 0) + 1
      room:sendLog{
        type = "神徐盛选择了此【杀】额外结算一次",
        from = player.id,
        arg = hx__kuijun.name
      }
    elseif choice == "#hx__kuijun_slash" then
      room:addTableMark(player, "@hx__kuijun_select-turn", 3)
      room:addPlayerMark(player, "@hx__kuijun_slash", 1)
      room:sendLog{
        type = "神徐盛选择了此【杀】结算后增加一次出杀次数",
        from = player.id,
        arg = hx__kuijun.name
      }
    elseif choice == "#hx__kuijun_get" and not data.card:isVirtual() then
      room:addTableMark(player, "@hx__kuijun_select-turn", 4)
      room:obtainCard(player, data.card.id, true, fk.ReasonGive)
      room:sendLog{
        type = "神徐盛选择了获得此【杀】",
        from = player.id,
        arg = hx__kuijun.name
      }
    end
  end,
})

hx__kuijun:addEffect("targetmod",{
  name = "#hx__kuijun_chusha",
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill("hx__kuijun") and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return player:getMark("@hx__kuijun_slash")
    end
    return 0
  end,
})

hx__kuijun:addEffect("filter", {
  anim_type = "special",
  card_filter = function(self, card, player, isJudgeEvent)
    return player:hasSkill(hx__kuijun.name) and (card.name == "analeptic" or card.name == "peach") and
      (table.contains(player.player_cards[Player.Hand], card.id) or isJudgeEvent)
  end,
  view_as = function(self, player, to_select)
    if to_select.name == "analeptic" then
      return Fk:cloneCard("peach", to_select.suit, to_select.number)
    elseif to_select.name == "peach" then
      return Fk:cloneCard("analeptic", to_select.suit, to_select.number)
    end
  end,
})



hx__kuijun:addEffect("atkrange", {
  correct_func = function(self, from, to)
    if from:hasSkill(hx__kuijun.name) then
      return 3
    end
  end,
})


return hx__kuijun
